package com.lgy.controller;

import com.github.pagehelper.PageInfo;
import com.lgy.pojo.Adminlog;
import com.lgy.pojo.Company;
import com.lgy.pojo.Employee;
import com.lgy.service.IAdminLogService;
import com.lgy.service.ICompanyService;
import com.lgy.service.IEmployeeService;
import com.lgy.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/system")
public class SystemController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IAdminLogService adminLogService;

    //system/getAllEmp.do?
    @RequestMapping("/getAdminAllEmp.do")
    public String getAllEmp(Model model, HttpSession session,@RequestParam(value = "page",defaultValue = "1") Integer page,@RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize) {
        Employee emp = (Employee) session.getAttribute("emp");
        model.addAttribute("pageInfo",employeeService.findEmpAndPositionName(emp.getCompanyid(),page,pageSize));
        return "AdminEmpList";
    }

    //修改
    @RequestMapping("/getEmployeeById.do")
    @ResponseBody
    public ResultData<Employee> getEmployeeById(@RequestParam("empId")Integer empid) {
        Employee employee = employeeService.findByEmpId(empid);
        if(employee!=null){
            return ResultData.ok(employee);
        }
        return ResultData.error("获取失败");
    }
    //system/updateEmployee.do
    @RequestMapping("/updateEmployee.do")
    public String updateEmployee(Employee employee) {
        employeeService.updateByPrimaryKeySelective(employee);
        return "redirect:/system/getAdminAllEmp.do";
    }

    //删除员工
    @RequestMapping("/deleteEmployeeById.do")
    public String deleteEmployeeById(@RequestParam("empId")Integer empid) {
        Employee employee = employeeService.findByEmpId(empid);
        employeeService.deleteByPrimaryKey(empid);
        return "redirect:/system/getAdminAllEmp.do";
    }

    //insertEmployee.do
    @RequestMapping("/insertEmployee.do")
    public String insertEmployee(Employee employee) {
        int row = employeeService.insert(employee);
        return "redirect:/system/getAdminAllEmp.do";
    }
//------------------------------------日志-------------------------------------
    //日志相关
    ///system/getAllLog.do
    @RequestMapping("/getAllLog.do")
    public String getAllLog(Model model,HttpSession session,
                            @RequestParam(value = "page",defaultValue = "1") Integer page,
                            @RequestParam(value = "pageSize",defaultValue = "8")Integer pageSize){
        Employee emp = (Employee) session.getAttribute("emp");
        Integer companyId = emp.getCompanyid();
        PageInfo<Adminlog> pageInfo = adminLogService.findAllByCompanyId(companyId, page, pageSize);
        model.addAttribute("pageInfo",pageInfo);
        return "AdminLogList";
    }
}